/*
========================================================================
 Name        : AutoKeyLockAppUi.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <eikmenub.h>
#include <akncontext.h>
#include <akntitle.h>
#include <AutoKeyLock.rsg>
// ]]] end generated region [Generated System Includes]
#include <aknmessagequerydialog.h> 
// [[[ begin generated region: do not modify [Generated User Includes]
#include "AutoKeyLockAppUi.h"
#include "AutoKeyLockSettings.hrh"
#include "AutoKeyLock.hrh"
#include "AutoKeyLockSettingsView.h"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * Construct the CAutoKeyLockAppUi instance
 */ 
CAutoKeyLockAppUi::CAutoKeyLockAppUi()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * The appui's destructor removes the container from the control
 * stack and destroys it.
 */
CAutoKeyLockAppUi::~CAutoKeyLockAppUi()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	if(iActivityManager)
		{
		delete iActivityManager;
		iActivityManager = NULL;
		}
	}

// [[[ begin generated function: do not modify
void CAutoKeyLockAppUi::InitializeContainersL()
	{
	iAutoKeyLockSettingsView = CAutoKeyLockSettingsView::NewL();
	AddViewL( iAutoKeyLockSettingsView );
	SetDefaultViewL( *iAutoKeyLockSettingsView );
	}
// ]]] end generated function

/**
 * Handle a command for this appui (override)
 * @param aCommand command id to be handled
 */
void CAutoKeyLockAppUi::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{ // code to dispatch to the AppUi's menu and CBA commands is generated here
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
		if ( aCommand == EAknSoftkeyExit || aCommand == EEikCmdExit )
			{
			Exit();
			}
		}
	// ]]] end generated region [Generated Code]
	
	if ( !commandHandled ) 
		{
		switch ( aCommand )
			{ 
			case EAutoKeyLockSettingsViewAboutMenuItemCommand:
				ShowAboutL();
				break;
			case EAutoKeyLockSettingsViewAutoKeyLockHideCmd:
				SendToBack();
				HideIconFromTaskList(ETrue);
				break;
			default:
				break;
			}		
		}
	}

/** 
 * Override of the HandleResourceChangeL virtual function
 */
void CAutoKeyLockAppUi::HandleResourceChangeL( TInt aType )
	{
	CAknViewAppUi::HandleResourceChangeL( aType );
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}
				
/** 
 * Override of the HandleKeyEventL virtual function
 * @return EKeyWasConsumed if event was handled, EKeyWasNotConsumed if not
 * @param aKeyEvent 
 * @param aType 
 */
TKeyResponse CAutoKeyLockAppUi::HandleKeyEventL(
		const TKeyEvent& aKeyEvent,
		TEventCode aType )
	{
	// The inherited HandleKeyEventL is private and cannot be called
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	return EKeyWasNotConsumed;
	}

/** 
 * Override of the HandleViewDeactivation virtual function
 *
 * @param aViewIdToBeDeactivated 
 * @param aNewlyActivatedViewId 
 */
void CAutoKeyLockAppUi::HandleViewDeactivation( 
		const TVwsViewId& aViewIdToBeDeactivated, 
		const TVwsViewId& aNewlyActivatedViewId )
	{
	CAknViewAppUi::HandleViewDeactivation( 
			aViewIdToBeDeactivated, 
			aNewlyActivatedViewId );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * @brief Completes the second phase of Symbian object construction. 
 * Put initialization code that could leave here. 
 */ 
void CAutoKeyLockAppUi::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	BaseConstructL( EAknEnableSkin ); 
	InitializeContainersL();
	// ]]] end generated region [Generated Contents]
	iAppSettings = CAppSettings::NewL();
	iAppSettings->InternalizeSettingsDataL();
	}

void CAutoKeyLockAppUi::ActivityDetected()
	{
	
	}

void CAutoKeyLockAppUi::InactivityDetected()
	{
	if(iAppSettings->AutoKeyLock())
		{
		LockKey();
		}
	}

#include <aknkeylock.h>  // RAknKeyLock
void CAutoKeyLockAppUi::LockKey()
	{
	RAknKeyLock keyLock;
	User::LeaveIfError(keyLock.Connect());
	if (!keyLock.IsKeyLockEnabled())
	    {
	    keyLock.EnableKeyLock();
	    }
	}

void CAutoKeyLockAppUi::SendToBack()
	{
	TApaTask task(CEikonEnv::Static()->WsSession());
	task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());
	task.SendToBackground();
	}

void CAutoKeyLockAppUi::HideIconFromTaskList(TBool aHide)
	{
	RWsSession session = CEikonEnv::Static()->WsSession();
	TInt wgId = CEikonEnv::Static()->RootWin().Identifier();
    CApaWindowGroupName* wgName = CApaWindowGroupName::NewLC(session, wgId);
    wgName->SetHidden(aHide);
    wgName->SetWindowGroupName(CEikonEnv::Static()->RootWin());
    CleanupStack::PopAndDestroy(); // wgName
	}

void CAutoKeyLockAppUi::HandleForegroundEventL(TBool aForeground)
	{
	if(aForeground)
		{
		HideIconFromTaskList(EFalse);
		if(!iActivityManager)
			{
			delete iActivityManager;
			iActivityManager = NULL;
			}
		if(iAppSettings->AutoKeyLock())
			{
			iActivityManager = CActivityManager::NewL(this, iAppSettings->LapseTime());
			iActivityManager->Start();
			}
		}
	CAknAppUi::HandleForegroundEventL(aForeground);
	}

TBool CAutoKeyLockAppUi::ProcessCommandParametersL( CApaCommandLine &aCommandLine )
	{
	if(aCommandLine.OpaqueData().Length() > 0)
		{
		// Opaque data exists, app. has been manually started from the menu
		}
	else
		{
		// App. has been auto-started -> exit if auto-start in settings is OFF
		if(iAppSettings->AutoStart())
			{
			SendToBack();
			}
		else
			{
			Exit();
			}
		}
	return CEikAppUi::ProcessCommandParametersL( aCommandLine );
	}

void CAutoKeyLockAppUi::ShowAboutL()
	{
	_LIT(KAboutMsg, "Auto Key Lock \nVersion 0.01 \n\nCopyright 2009-2010,\nAll right reserved.\nContact author via email\nsunil@skumar.me \nHomepage: www.skumar.me");
	_LIT(KApplicationName,"Auto Key Lock");
	 
	HBufC* about = KAboutMsg().AllocLC();
	TPtr aboutPointer(about->Des());
	CAknMessageQueryDialog* messageQueryDialog = CAknMessageQueryDialog::NewL(aboutPointer);
	messageQueryDialog->PrepareLC(R_ABOUT_HEADING_PANE);
	messageQueryDialog->SetHeaderTextL(KApplicationName);  
	messageQueryDialog->RunLD();
	 
	CleanupStack::PopAndDestroy(about);
	}

